﻿using AutoMapper;

using Microsoft.IdentityModel.Tokens;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;
using ZFB.Repository.Region;

namespace ZFB.Services
{
    public class RegionServices : IRegionServices
    {
        private readonly IRegionRepository regionRepository;
        private readonly IMapper mapper;

        public RegionServices(IRegionRepository regionRepository, IMapper mapper)
        {
            this.regionRepository = regionRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> GetAllAsync(long cityid)
        {
          var data= await regionRepository.GetListAsync(e => e.CityId== cityid);
            if (data.IsNullOrEmpty()) return new("城市id不存在");
            var dto=mapper.Map<List<RegionDTO>>(data);
            return new(dto);
        }

        public async Task<ApiResult> GetByIdAsync(long id)
        {
            var data = await regionRepository.GetListAsync(e => e.Id == id);
            if (data.IsNullOrEmpty()) return new("id不存在");
            var dto = mapper.Map<List<RegionDTO>>(data);
            return new(dto);
        }
    }
}
